/*
 * Assignment3part1.cpp - Timing Example - comparing ArrayList and LinkedList
 *
 *  Created on: Mar 25, 2014
 *      Author: Anthony Varghese
 */

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::flush;


void start_clock();
void stop_clock();

#include "ArrayList.h"
#include "LinkedList.h"
using cs237list::ArrayList;
using cs237list::LinkedList;

int main(){
	cout << "CSIS 237 Assignment 3 part 1 - Timing" << endl;

	const int MIN_PROBLEM_SIZE = 1;
	const int MAX_PROBLEM_SIZE = 1024 * 1024;

	for (unsigned int size=MIN_PROBLEM_SIZE; size<=MAX_PROBLEM_SIZE; size=size*2){
	   /*
	    * set up random data for sorting
	    */
	   ArrayList a( size );
	   a.fillRandom(0.0 , 100.0);


	   cout << "Sorting (size=" << size << ") ... " << flush;
	   /*
	    * Start clock, sort, and stop clock.
	    */

	   start_clock();

	   a.sort();

	   stop_clock();


	   cout << "... done sorting!" << endl;
	}
	cout << "--------------------------------------------------------------" << endl;

	for (unsigned int size=MIN_PROBLEM_SIZE; size<=MAX_PROBLEM_SIZE; size=size*2){
	   /*
	    * set up random data for sorting
	    */
	   LinkedList b;
	   b.fillRandom( size, 0.0 , 100.0);

	   cout << "Sorting (size=" << size << ") ... " << flush;
	   /*
	    * Start clock, sort, and stop clock.
	    */

	   start_clock();

	   b.sort();

	   stop_clock();


	   cout << "... done sorting!" << endl;
	}
}





/*
 * Timing for Linux
 */
#include <sys/time.h>
#include <sys/resource.h>
timeval start_timevalue_user;
timeval start_timevalue_system;
void start_clock(){
	rusage  resource_usage;
	getrusage( RUSAGE_SELF, &resource_usage );
	start_timevalue_user   = resource_usage.ru_utime;
	start_timevalue_system = resource_usage.ru_stime;
}

void stop_clock(){
	rusage  resource_usage;
	getrusage( RUSAGE_SELF, &resource_usage );
	timeval end_timevalue_user   = resource_usage.ru_utime;
	timeval end_timevalue_system = resource_usage.ru_stime;

	double end_tu = end_timevalue_user  .tv_sec + end_timevalue_user  .tv_usec / 1.0e6;
	double end_ts = end_timevalue_system.tv_sec + end_timevalue_system.tv_usec / 1.0e6;

	double start_tu = start_timevalue_user  .tv_sec + start_timevalue_user  .tv_usec / 1.0e6;
	double start_ts = start_timevalue_system.tv_sec + start_timevalue_system.tv_usec / 1.0e6;

	cout << "   User time:   " << (end_tu - start_tu)  << "\t\tSystem time: " << (end_ts - start_ts);
}
